﻿using AutoMapper;
using BW.MesProject.Production.Domain.Dto.Input.ProcessManagement;
using BW.MesProject.Production.Domain.Dto.Output.ProcessManagement;
using BW.MesProject.Production.Domain.Model;
using BW.MesProject.Production.Infrastructure;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.MesProject.Production.Application
{
    public class ProcessManagementServices : IProcessManagementServices
    {
        private readonly IBaseRepository<PROCESSMANAGEMENTMODEL> _processManagementRepository;
        private readonly IMapper _mapper;

        public ProcessManagementServices(IBaseRepository<PROCESSMANAGEMENTMODEL> processManagementRepository, IMapper mapper)
        {
            _processManagementRepository = processManagementRepository;
            _mapper = mapper;
        }

        public ApiResult<int> CreateProcessManagement(CreateProcessManagementInput input)
        {
            var model = _mapper.Map<PROCESSMANAGEMENTMODEL>(input);
            model.CREATETIME = DateTime.Now;
            model.ISDELETED = false;
            model.CREATEUSERNAME = "admin"; // Replace with actual user context
            model.PROCESSMANAGEMENTCODE = "PM" + DateTime.Now.ToString("yyyyMMdd") + new Random().Next(1000, 9999).ToString();
            int result = _processManagementRepository.Add(model);

            if (result > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Success,
                    Message = "工序创建成功",
                    Data = result
                };
            }
            return new ApiResult<int>
            {
                Code = ApiEnums.Error,
                Message = "工序创建失败",
                Data = result
            };
        }

        public ApiResult<int> DeleteProcessManagement(long Id)
        {
            var model = _processManagementRepository.GetAll().First(x=>x.PROCESSMANAGEMENTID == Id);
            if (model == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Message = "工序不存在或已被删除",
                    Data = 0
                };
            }

            int r = _processManagementRepository.Delete(model);
            if (r > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Success,
                    Message = "工序删除成功",
                    Data = r
                };
            }
            return new ApiResult<int>
            {
                Code = ApiEnums.Error,
                Message = "工序删除失败",
                Data = r
            };
        }

        public ApiResult<List<ProcessManagementListOutput>> GetProcessManagementList()
        {
            var processManagements = _processManagementRepository.GetAll().Where(x => !x.ISDELETED);
            if (!processManagements.Any())
            {
                return new ApiResult<List<ProcessManagementListOutput>>
                {
                    Code = ApiEnums.Fail,
                    Message = "没有找到任何工序",
                    Data = new List<ProcessManagementListOutput>()
                };
            }

            var outputs = _mapper.Map<List<ProcessManagementListOutput>>(processManagements.ToList());
            return new ApiResult<List<ProcessManagementListOutput>>
            {
                Code = ApiEnums.Success,
                Message = "工序列表获取成功",
                Data = outputs,
                TotalCount = processManagements.Count()
            };
        }

        public ApiResult<int> UpdateProcessManagement(UpdateProcessManagementInput input)
        {
            var model = _processManagementRepository.GetAll().First(x => x.PROCESSMANAGEMENTID == input.PROCESSMANAGEMENTID);
            if (model == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Message = "工序不存在",
                    Data = 0
                };
            }
            model = _mapper.Map(input, model);
            model.UPDATETIME = DateTime.Now;
            model.UPDATEUSERNAME = "admin"; // Replace with actual user context

            int result = _processManagementRepository.Update(model);
            if (result > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Success,
                    Message = "工序更新成功",
                    Data = result
                };
            }
            return new ApiResult<int>
            {
                Code = ApiEnums.Error,
                Message = "工序更新失败",
                Data = result
            };
        }
    }
}
